﻿namespace AnyOffice.DAL
{
    using AnyOffice.Components;
    using AnyOffice.Model;
    using log4net;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Web;

    public class DepartmentDao
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public bool Delete(SqlConnection conn, int deptId)
        {
            string commandText = "DECLARE @RET bit IF EXISTS(SELECT [UserId] FROM [dbo].[Users] WHERE [DeptId] = @DeptId) SET @RET = 0 ELSE BEGIN DELETE FROM [dbo].[Department] WHERE [DeptId] = @DeptId SET @RET = 1 END SELECT @RET";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DeptId", SqlDbType.Int) };
            commandParameters[0].Value = deptId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 == null)
            {
                return false;
            }
            if (!Convert.ToBoolean(obj2))
            {
                return false;
            }
            ConfigManager manager = new ConfigManager();
            if (manager.RtxEnabled)
            {
                try
                {
                    Type type = Assembly.LoadFrom(HttpContext.Current.Server.MapPath("/bin/RTX.dll")).GetType("RTX.RtxSdk", true);
                    object obj3 = Activator.CreateInstance(type);
                    MethodInfo method = type.GetMethod("DelDept");
                    object[] parameters = new object[] { deptId };
                    method.Invoke(obj3, parameters);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("RTX", exception);
                    }
                }
            }
            return true;
        }

        public int Insert(SqlConnection conn, DepartmentData data)
        {
            string commandText = "IF NOT EXISTS(SELECT [DeptId] FROM [dbo].[Department] WHERE [ParentDept] = @ParentDept AND [DeptName] = @DeptName) BEGIN INSERT INTO [dbo].[Department] ( [ParentDept], [DeptName], [DeptNo], [TelNo], [FaxNo], [Manager], [Leader1], [Leader2], [DeptFunc] ) VALUES ( @ParentDept, @DeptName, @DeptNo, @TelNo, @FaxNo, @Manager, @Leader1, @Leader2, @DeptFunc ) SELECT SCOPE_IDENTITY() AS RetValue END ELSE SELECT -1";
            SqlParameter[] commandParameters = new SqlParameter[9];
            commandParameters[0] = new SqlParameter("@ParentDept", SqlDbType.Int);
            commandParameters[0].Value = data.ParentDept;
            commandParameters[1] = new SqlParameter("@DeptName", SqlDbType.NVarChar, 50);
            commandParameters[1].Value = data.DeptName;
            commandParameters[2] = new SqlParameter("@DeptNo", SqlDbType.Int);
            commandParameters[2].Value = data.DeptNo;
            commandParameters[3] = new SqlParameter("@TelNo", SqlDbType.NVarChar, 50);
            if (data.TelNoIsNull)
            {
                commandParameters[3].Value = DBNull.Value;
            }
            else
            {
                commandParameters[3].Value = data.TelNo;
            }
            commandParameters[4] = new SqlParameter("@FaxNo", SqlDbType.NVarChar, 50);
            if (data.FaxNoIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.FaxNo;
            }
            commandParameters[5] = new SqlParameter("@Manager", SqlDbType.NText);
            if (data.ManagerIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.Manager;
            }
            commandParameters[6] = new SqlParameter("@Leader1", SqlDbType.NText);
            if (data.Leader1IsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.Leader1;
            }
            commandParameters[7] = new SqlParameter("@Leader2", SqlDbType.NText);
            if (data.Leader2IsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.Leader2;
            }
            commandParameters[8] = new SqlParameter("@DeptFunc", SqlDbType.NText);
            if (data.DeptFuncIsNull)
            {
                commandParameters[8].Value = DBNull.Value;
            }
            else
            {
                commandParameters[8].Value = data.DeptFunc;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 == null)
            {
                return -1;
            }
            int num = Convert.ToInt32(obj2);
            if (num == -1)
            {
                return -1;
            }
            ConfigManager manager = new ConfigManager();
            if (manager.RtxEnabled)
            {
                try
                {
                    Type type = Assembly.LoadFrom(HttpContext.Current.Server.MapPath("/bin/RTX.dll")).GetType("RTX.RtxSdk", true);
                    object obj3 = Activator.CreateInstance(type);
                    MethodInfo method = type.GetMethod("AddDept");
                    object[] parameters = new object[] { num, data.ParentDept, data.DeptName, data.DeptFunc };
                    method.Invoke(obj3, parameters);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("RTX", exception);
                    }
                }
            }
            return num;
        }

        public bool IsExistsChild(SqlConnection conn, int deptId)
        {
            string commandText = "DECLARE @RET bit IF EXISTS(SELECT [DeptId] FROM [dbo].[Department] WHERE [ParentDept] = @DeptId) SET @RET = 1 ELSE SET @RET = 0 SELECT @RET";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DeptId", SqlDbType.Int) };
            commandParameters[0].Value = deptId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            return ((obj2 != null) && Convert.ToBoolean(obj2));
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            string commandText = "SELECT * FROM [dbo].[Department] WHERE [DeptId] <> -1 ORDER BY [DeptNo] ASC";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public DataTable SelectByDeptId(SqlConnection conn, int deptId)
        {
            string commandText = "SELECT * ,dbo.GetUserNames(Manager) AS ManagerName, dbo.GetUserNames(Leader1) AS Leader1Name,\tdbo.GetUserNames(Leader2) AS Leader2Name FROM [dbo].[Department] WHERE [DeptId] = @DeptId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DeptId", SqlDbType.Int) };
            commandParameters[0].Value = deptId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public DataTable SelectChildAll(SqlConnection conn, int parentDept)
        {
            string commandText = "SELECT * FROM [dbo].[Department] WHERE [ParentDept] = @ParentDept ORDER BY [DeptNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@ParentDept", SqlDbType.Int) };
            commandParameters[0].Value = parentDept;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public DataTable SelectLeader1(SqlConnection conn, int deptId)
        {
            string commandText = "SELECT [Users].[UserId], [Users].[UserName] FROM [dbo].[Department], [dbo].[Users] WHERE [Department].[DeptId] = @DeptId AND CHARINDEX(',' + [Users].[UserId] + ',',',' + ISNULL(CAST([Department].[Leader1] AS nvarchar(2000)),'') + ',') > 0 ORDER BY [Users].[UserNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DeptId", SqlDbType.Int) };
            commandParameters[0].Value = deptId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public DataTable SelectLeader2(SqlConnection conn, int deptId)
        {
            string commandText = "SELECT [Users].[UserId], [Users].[UserName] FROM [dbo].[Department], [dbo].[Users] WHERE [Department].[DeptId] = @DeptId AND CHARINDEX(',' + [Users].[UserId] + ',',',' + ISNULL(CAST([Department].[Leader2] AS nvarchar(2000)),'') + ',') > 0 ORDER BY [Users].[UserNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DeptId", SqlDbType.Int) };
            commandParameters[0].Value = deptId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public DataTable SelectManager(SqlConnection conn, int deptId)
        {
            string commandText = "SELECT [Users].[UserId], [Users].[UserName] FROM [dbo].[Department], [dbo].[Users] WHERE [Department].[DeptId] = @DeptId AND CHARINDEX(',' + [Users].[UserId] + ',',',' + ISNULL(CAST([Department].[Manager] AS nvarchar(2000)),'') + ',') > 0 ORDER BY [Users].[UserNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@DeptId", SqlDbType.Int) };
            commandParameters[0].Value = deptId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public DataTable SelectParentAll(SqlConnection conn)
        {
            string commandText = "SELECT * FROM [dbo].[Department] WHERE [ParentDept] = 0 AND [DeptId] <> -1 ORDER BY [DeptNo] ASC";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public int Update(SqlConnection conn, DepartmentData data)
        {
            string commandText = "IF NOT EXISTS(SELECT [DeptId] FROM [dbo].[Department] WHERE [ParentDept] = @ParentDept AND [DeptName] = @DeptName AND [DeptId] <> @DeptId) BEGIN UPDATE [dbo].[Department] SET [ParentDept] = @ParentDept, [DeptName] = @DeptName, [DeptNo] = @DeptNo, [TelNo] = @TelNo, [FaxNo] = @FaxNo, [Manager] = @Manager, [Leader1] = @Leader1, [Leader2] = @Leader2, [DeptFunc] = @DeptFunc WHERE [DeptId] = @DeptId SELECT @DeptId END ELSE SELECT -1";
            SqlParameter[] commandParameters = new SqlParameter[10];
            commandParameters[0] = new SqlParameter("@DeptId", SqlDbType.Int);
            commandParameters[0].Value = data.DeptId;
            commandParameters[1] = new SqlParameter("@ParentDept", SqlDbType.Int);
            commandParameters[1].Value = data.ParentDept;
            commandParameters[2] = new SqlParameter("@DeptName", SqlDbType.NVarChar, 50);
            commandParameters[2].Value = data.DeptName;
            commandParameters[3] = new SqlParameter("@DeptNo", SqlDbType.Int);
            commandParameters[3].Value = data.DeptNo;
            commandParameters[4] = new SqlParameter("@TelNo", SqlDbType.NVarChar, 50);
            if (data.TelNoIsNull)
            {
                commandParameters[4].Value = DBNull.Value;
            }
            else
            {
                commandParameters[4].Value = data.TelNo;
            }
            commandParameters[5] = new SqlParameter("@FaxNo", SqlDbType.NVarChar, 50);
            if (data.FaxNoIsNull)
            {
                commandParameters[5].Value = DBNull.Value;
            }
            else
            {
                commandParameters[5].Value = data.FaxNo;
            }
            commandParameters[6] = new SqlParameter("@Manager", SqlDbType.NText);
            if (data.ManagerIsNull)
            {
                commandParameters[6].Value = DBNull.Value;
            }
            else
            {
                commandParameters[6].Value = data.Manager;
            }
            commandParameters[7] = new SqlParameter("@Leader1", SqlDbType.NText);
            if (data.Leader1IsNull)
            {
                commandParameters[7].Value = DBNull.Value;
            }
            else
            {
                commandParameters[7].Value = data.Leader1;
            }
            commandParameters[8] = new SqlParameter("@Leader2", SqlDbType.NText);
            if (data.Leader2IsNull)
            {
                commandParameters[8].Value = DBNull.Value;
            }
            else
            {
                commandParameters[8].Value = data.Leader2;
            }
            commandParameters[9] = new SqlParameter("@DeptFunc", SqlDbType.NText);
            if (data.DeptFuncIsNull)
            {
                commandParameters[9].Value = DBNull.Value;
            }
            else
            {
                commandParameters[9].Value = data.DeptFunc;
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 == null)
            {
                return -1;
            }
            int num = Convert.ToInt32(obj2);
            if (num == -1)
            {
                return -1;
            }
            ConfigManager manager = new ConfigManager();
            if (manager.RtxEnabled)
            {
                try
                {
                    Type type = Assembly.LoadFrom(HttpContext.Current.Server.MapPath("/bin/RTX.dll")).GetType("RTX.RtxSdk", true);
                    object obj3 = Activator.CreateInstance(type);
                    MethodInfo method = type.GetMethod("SetDept");
                    object[] parameters = new object[] { num, data.ParentDept, data.DeptName, data.DeptFunc };
                    method.Invoke(obj3, parameters);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("RTX", exception);
                    }
                }
            }
            return num;
        }
    }
}

